﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Xml;

using DAL;

namespace WS
{
    /// <summary>
    /// Summary description for StoreService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class StoreService : System.Web.Services.WebService
    {

        [WebMethod]
        public XmlDocument GetStoresInfo()
        {

            using (AWCDataContext awcDC = new AWCDataContext())
            {

                XElement stores = new XElement("Stores",
                                      from s in awcDC.Stores
                                      join ca in awcDC.CustomerAddresses
                                      on s.CustomerID equals ca.CustomerID
                                      join co in awcDC.StoreContacts
                                      on s.CustomerID equals co.CustomerID
                                      where ca is CustomerAddress &&
                                          /*AdressTypeID3 = main office */
                                     (ca.AddressTypeID == 3/* && ca.Address.GeoCode.Latitude != "0" && ca.Address.GeoCode.Longitude != "0"*/)
                                      select new XElement("Store",
                                          new XElement("id", s.CustomerID),
                                          new XElement("Name", s.Name),
                                          new XElement("Address",
                                              new XElement("Postalcode",ca.Address.PostalCode ),
                                              new XElement("AddressLine1", ca.Address.AddressLine1),
                                              new XElement("AddressLine2", ca.Address.AddressLine2),
                                              new XElement("City", ca.Address.City),
                                              new XElement("State", ca.Address.StateProvince.Name),
                                              new XElement("Country", ca.Address.StateProvince.CountryRegionCode)
                                          ),
                                          new XElement("Contact",
                                              new XElement("FirstName", co.Contact.FirstName ),
                                              new XElement("MiddleName", co.Contact.MiddleName),
                                              new XElement("LastName", co.Contact.LastName),
                                              new XElement("Phone", co.Contact.Phone))));


                XmlDocument storesxml = new XmlDocument();
                storesxml.Load(stores.CreateReader());
                return storesxml;


            }


        }

        [WebMethod]
        public XmlDocument GetStoresInfoByCountry(string countryRegionCode)
        {

            using (AWCDataContext awcDC = new AWCDataContext())
            {
                XElement stores = new XElement("Stores",
                                      from s in awcDC.Stores
                                      join ca in awcDC.CustomerAddresses
                                      on s.CustomerID equals ca.CustomerID
                                      where ca is CustomerAddress &&
                                          /*AdressTypeID3 = main office */
                                     (ca.AddressTypeID == 3 && ca.Address.StateProvince.CountryRegionCode == countryRegionCode)
                                      select new XElement("Store",
                                          new XElement("id", s.CustomerID),
                                          new XElement("Name", s.Name),
                                          new XElement("Postalcode", ca.Address.PostalCode),
                                          new XElement("AddressLine1", ca.Address.AddressLine1),
                                          new XElement("AddressLine2", ca.Address.AddressLine2),
                                          new XElement("City", ca.Address.City),
                                          new XElement("State", ca.Address.StateProvince.Name),
                                          new XElement("Country", ca.Address.StateProvince.CountryRegionCode)
                                          ));
                //ca.Address.PostalCode,);

                XmlDocument storesxml = new XmlDocument();
                storesxml.Load(stores.CreateReader());
                return storesxml;


            }


        }
    }
}
